package com.yuanqunsoft.alimigration.action;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.json.JSONObject;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yuanqunsoft.alimigration.bean.RuleBean;
import com.yuanqunsoft.alimigration.bean.RuleSetBean;
import com.yuanqunsoft.alimigration.model.AllUser;
import com.yuanqunsoft.alimigration.model.Rule;
import com.yuanqunsoft.alimigration.model.RuleSet;
import com.yuanqunsoft.alimigration.service.IRuleService;
import com.yuanqunsoft.alimigration.service.IRuleSetService;
import com.yuanqunsoft.alimigration.utils.JsonResult;

public class RuleSetAction extends ActionSupport {


    private IRuleService ruleService;
    private IRuleSetService ruleSetService;
    private boolean success;
    private JsonResult result;
    private int rusId;
    private List<?> datas;
    private String selectedRules;
    public String Page() {
        return "page";
    }

    public String ruleSetList() throws Exception {
   
        int startNo = Integer.parseInt(ServletActionContext.getRequest().getParameter("start"));
        int pageSize = Integer.parseInt(ServletActionContext.getRequest().getParameter("limit"));  

        List<RuleSetBean> list = this.getRuleSetService().getAllRuleSets(startNo, pageSize);
        setResult(new JsonResult(list, this.ruleSetService.getAllPage()));
        this.setSuccess(true);
        return "success";
    }
    
    
    public String delRuleSet() throws Exception {
        // TODO Auto-generated method stub
        try {
            RuleSetBean rulesetBean = this.getRuleSetService().getRuleSetById(rusId);
            this.getRuleSetService().deleteRuleSet(rulesetBean);
            this.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            this.setSuccess(false);
        }
        return "success";
    }
    
    /**
     * 为规则集添加规则
     * @return
     * @throws Exception
     */
    public String conRules() throws Exception {
        // TODO Auto-generated method stub
        
        JSONObject json = new JSONObject(datas.get(0).toString());
        List<Integer> ids = new ArrayList<Integer>();
        for(int i = 0; i < json.length(); i++){
            ids.add(Integer.parseInt(json.getString("" + i).toString()));
        }
        RuleSetBean rs = this.ruleSetService.getRuleSetById(rusId);
        this.ruleSetService.selectRules(rs, ids);
        this.setSuccess(true);
        return "success";
    }

    public String getRules() throws Exception {
        // TODO Auto-generated method stub
        List<RuleBean> list = this.ruleService.getAllRule();
        setResult(new JsonResult(list, list.size()));
        this.setSuccess(true);
        return "success";
    }
    
    public String showSelectedRules() throws Exception {
        // TODO Auto-generated method stub

        List<RuleBean> rules = this.ruleSetService.getRuleByRuleSetId(rusId);
        List<RuleBean> allRules = this.ruleService.getAllRule();
        List<String> rs = new ArrayList<String>();
        for(int i = 0; i < rules.size(); i++){
            for(int j = 0; j < allRules.size(); j++){
                if(rules.get(i).getRulId() == allRules.get(j).getRulId()){
                    rs.add(j + "");
                }
            }
        }
//        this.selectedRules = "{";
//        for(int i = 0; i < rs.size(); i++){
//            if(i == 0){
//                this.selectedRules =this.selectedRules + "\"" + i + "\":\"" + rs.get(i) + "\"";
//            }
//            else{
//                this.selectedRules =this.selectedRules + ",\"" + i + "\":\"" + rs.get(i) + "\"";
//            }
//        }
        this.selectedRules = "";
        for(int i = 0; i < rs.size(); i++){
            if(i == 0){
                this.selectedRules = this.selectedRules + rs.get(i);
            }
            else {
                this.selectedRules = this.selectedRules + "," + rs.get(i);
            }
        }
        
        System.out.println(this.selectedRules);
        this.setSuccess(true);
        return "success";
    }
    
    public String addRuleSet() {
        JSONObject json = new JSONObject(datas.get(0).toString());// 转换为json对象格式

        AllUser user = (AllUser)ActionContext.getContext().getSession().get("webUser");
       
        // 将json对象转换为javaBean
        RuleSetBean ruleSet = new RuleSetBean();
        String rusName = json.getString("rusName");
        String rusDescription = json.getString("rusDescription");
        String rusVersion = json.getString("rusVersion");
        String rusPublic = json.getString("rusPublic");
      
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        ruleSet.setRusName(rusName);
        ruleSet.setRusDescription(rusDescription);
        ruleSet.setRusVersion(Integer.parseInt(rusVersion));
        ruleSet.setRusCreateTime(timestamp);
        ruleSet.setRusPublic(Short.valueOf(rusPublic.toString()));
        ruleSet.setUserName(user.getUserName());
        ruleSet.setUserId(user.getUserId());
        if(this.ruleSetService.isRuleSetNameExisted(ruleSet)){
            this.setSuccess(false);
            return "success";
        }
        this.ruleSetService.addRuleSet(ruleSet, null);
        this.setSuccess(true);
        return "success";

    }

    public String updateRuleSet() {
        JSONObject json = new JSONObject(datas.get(0).toString()); // 转换为json对象格式
        try {

            RuleSetBean ruleSet = new RuleSetBean();
            String rusName = json.getString("rusName");
            String rusDescription = json.getString("rusDescription");
            String rusVersion = json.getString("rulVersion");
            ruleSet.setRusName(rusName);
            ruleSet.setRusDescription(rusDescription);
            ruleSet.setRusVersion(Integer.parseInt(rusVersion));

            this.ruleSetService.updateRuleSet(ruleSet);
            this.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            this.setSuccess(false);
        }
        return "success";
    }

    public IRuleSetService getRuleSetService() {
        return ruleSetService;
    }

    public void setRuleSetService(IRuleSetService ruleSetService) {
        this.ruleSetService = ruleSetService;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public JsonResult getResult() {
        return result;
    }

    public void setResult(JsonResult result) {
        this.result = result;
    }

    public int getRusId() {
        return rusId;
    }

    public void setRusId(int rusId) {
        this.rusId = rusId;
    }

    public List<?> getDatas() {
        return datas;
    }

    public void setDatas(List<?> datas) {
        this.datas = datas;
    }

    public IRuleService getRuleService() {
        return ruleService;
    }

    public void setRuleService(IRuleService ruleService) {
        this.ruleService = ruleService;
    }

    public String getSelectedRules() {
        return selectedRules;
    }

    public void setSelectedRules(String selectedRules) {
        this.selectedRules = selectedRules;
    }
    
    
    
    
}
